package Dji;

/**
 * Description：
 * Author: zhangc
 * Date：2017/9/14 10:18
 */

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;

public class Main {

    private static final int MIN = Integer.MIN_VALUE;
    private TreeNode head;
    private static List<TreeNode> nodeList;

    private Main(TreeNode node) {
        this.head = node;
    }

    public Main() {
    }

    private static class TreeNode {
        private Integer value;
        private TreeNode leftChild;
        private TreeNode rightChild;

        TreeNode(Integer value) {
            this.value = value;
        }
    }

    public int preOrderTraverseNoRecursive(TreeNode nodeH) {
        Set<Integer> set = new HashSet<>();
        List<Integer> list = new ArrayList<>();
        if (nodeH == null)
            return 0;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(nodeH);
        TreeNode temp;
        while (!stack.isEmpty()) {
            temp = stack.pop();
            set.add(temp.value);
            if (temp.rightChild != null && temp.rightChild.value != MIN)
                stack.push(temp.rightChild);
            if (temp.leftChild != null && temp.leftChild.value != MIN)
                stack.push(temp.leftChild);
        }
        return set.size();
    }

    public static Main createBinaryTreeByArrayIndexNoRecursive(List<Integer> list) {

        nodeList = new ArrayList<>();

        for (Integer aList : list) {
            nodeList.add(new TreeNode(aList));
        }
        for (int parentIndex = 0; parentIndex <= (list.size() >> 1) - 1; parentIndex++) {
            nodeList.get(parentIndex).leftChild = nodeList.get(parentIndex * 2 + 1);
            if ((parentIndex * 2 + 2) < list.size()) {
                nodeList.get(parentIndex).rightChild = nodeList.get(parentIndex * 2 + 2);
            }
        }
        return new Main(nodeList.get(0));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Integer total = 2 * scanner.nextInt() + 2;
        Integer[] arr = new Integer[ total ];
        arr[ 0 ] = 1;
        for (int i = 1; i < total; ++i)
            arr[ i ] = MIN;
        int no, father, lr, index;
        String string = null;
        for (int i = 1; i < (total - 2) >> 1; ++i) {
            no = scanner.nextInt();
            father = scanner.nextInt();
            lr = scanner.nextInt();

            index = getIndex(arr, father);
            if (lr == -1) {
                arr[ 2 * index + 1 ] = no;
            } else {
                arr[ 2 * index + 2 ] = no;
            }
        }
        List<Integer> list = Arrays.asList(arr);
        Main binaryTree = createBinaryTreeByArrayIndexNoRecursive(list);
        int node = scanner.nextInt();
        scanner.close();
        int index2 = getIndex(arr, node);
        int i = binaryTree.preOrderTraverseNoRecursive(nodeList.get(index2));
        System.out.println(i);
//        int node;
//        for (int i = 1; i < 12; i++) {
//            node = i;
//            int index2 = getIndex(arr, node);
//            int ii = binaryTree.preOrderTraverseNoRecursive(nodeList.get(index2));
//            System.out.println(ii);
//        }
    }

    public static int getIndex(Integer[] arr, int no) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[ i ].intValue() == no) {
                return i;
            }
        }
        return -1;
    }

//    @Test
//    public void Test() {
//        Integer total = 2 * 5 + 2;
//        //int[][] arrar = {{2, 1, -1}, {3, 1, 1}, {4, 2, -1}, {5, 2, 1}, {6, 3, -1}, {7, 3, 1}, {8, 4, -1}, {9, 4, 1}, {10, 5, 1}, {11, 7, 1}};
//        int[][] arrar = {{2, 1, -1}, {4, 2, -1}, {5, 4, -1}, {3, 1, 1}};
//        Integer[] arr = new Integer[ total ];
//        arr[ 0 ] = 1;
//        for (int i = 1; i < total; ++i)
//            arr[ i ] = 0;
//        int no, father, lr, index;
//        for (int i = 0; i < 4; ++i) {
//            no = arrar[ i ][ 0 ];
//            father = arrar[ i ][ 1 ];
//            lr = arrar[ i ][ 2 ];
//
//            index = getIndex(arr, father);
//            if (lr == -1) {
//                arr[ 2 * index + 1 ] = no;
//            } else {
//                arr[ 2 * index + 2 ] = no;
//            }
//        }
//        List<Integer> list = Arrays.asList(arr);
//        Main binaryTree = createBinaryTreeByArrayIndexNoRecursive(list);
//        int node;
//        for (int i = 1; i < 6; i++) {
//            node = i;
//            int index2 = getIndex(arr, node);
//            int ii = binaryTree.preOrderTraverseNoRecursive(nodeList.get(index2));
//            System.out.println(ii);
//        }

    //  }
}
